import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import userList from '../components/user/userList.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: login },
  { path: '/', redirect: '/login' },
  { path: '/home', 
  component: home ,
  redirect: "/welcome",
  children: [
    { path: '/welcome', component: welcome },
    { path: '/userList', component: userList }
  ]
}
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // next() 放行
  if(to.path == '/login'){
    return next()
  }else{
    const tokenStr = window.sessionStorage.getItem('token');
    if(!tokenStr){
      return next('/login')
    } 
    next()
  }
  
})

export default router
